package com.ruoyi.web.controller.erp.safetyinstruction;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.erp.safetyinstruction.domain.SafetyInstructionTab;
import com.ruoyi.erp.safetyinstruction.service.SafetyInstructionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @version 2017.1.1
 * @Auther Du Ruifang @萌えキュンキュン
 * @Date 2020/12/25 10:11
 * @description 该类的作用
 */
@Controller
@RequestMapping("/erp/safety")
public class SafetyInstructionController extends BaseController {
    @Autowired
    private SafetyInstructionService safetyInstructionService;
    private String prefix = "erp/safetyinstruction";

    /**
     * 跳转安全代码页面
     * @return
     */
    @RequiresPermissions("erp:safety:view")
    @GetMapping("/view")
    public String list (){
        return prefix+"/list";
    }

    /**
     * 查询安全代码列表
     * @param safetyInstructionTab
     * @return
     */
    @RequiresPermissions("erp:safety:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo getList (SafetyInstructionTab safetyInstructionTab) {
        List<SafetyInstructionTab> safetyInstructionTabList = safetyInstructionService.getList(safetyInstructionTab);
        return getDataTable(safetyInstructionTabList);
    }

    /**
     * 跳转添加页面
     * @return
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 添加安全代码数据
     * @param safetyInstructionTab
     * @return
     */
    @RequiresPermissions("erp:safety:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult saveSafetyInstruction (SafetyInstructionTab safetyInstructionTab){
        try {
            safetyInstructionService.saveSafetyInstruction(safetyInstructionTab);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("添加失败");
        }
        return AjaxResult.success("添加成功");
    }

    /**
     * 根据安全代码查询详情
     * @param hazardCode
     * @param mmap
     * @return
     */
    @GetMapping("/edit/{id}")
    public String findSafetyInstruction(@PathVariable("id") String hazardCode, ModelMap mmap)
    {
        mmap.put("SafetyInstruction", safetyInstructionService.findSafetyInstruction(hazardCode));
        return prefix + "/edit";
    }
    /**
     * 修改保存安全代码
     */
    @RequiresPermissions("erp:safety:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSaveSafetyInstruction(SafetyInstructionTab safetyInstructionTab){
        try {
            safetyInstructionService.editSaveSafetyInstruction(safetyInstructionTab);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            return AjaxResult.error("修改保存失败");
        }
        return AjaxResult.success("修改保存成功");
    }

    /**
     * 删除安全代码和批量删除
     * @param ids
     * @return
     */
    @RequiresPermissions("erp:safety:remove")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(safetyInstructionService.deleteSafetyInstruction(ids));
    }

    /**
     * 导出安全代码文档
     * @param safetyInstructionTab
     * @return
     */
    @RequiresPermissions("erp:safety:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SafetyInstructionTab safetyInstructionTab)
    {
        List<SafetyInstructionTab> safetyInstructionTabList = safetyInstructionService.getList(safetyInstructionTab);
        ExcelUtil<SafetyInstructionTab> util = new ExcelUtil<SafetyInstructionTab>(SafetyInstructionTab.class);
        return util.exportExcel(safetyInstructionTabList, "交货路线数据");
    }
}
